package main

import "fmt"

func main() {
	fmt.Println(captureForts([]int{1, 0, 0, -1, 0, 0, 0, 0, 1}))
}

// 只需一次遍历即可，保存上次不为0的位置就行
func captureForts(forts []int) (ans int) {
	var pre = -1
	for i, n := range forts {
		if n != 0 {
			if pre >= 0 && forts[pre] != n {
				ans = max4(ans, i-pre-1)
			}
			pre = i
		}
	}
	return
}

func max4(a, b int) int {
	if a > b {
		return a
	}
	return b
}
